package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.RConst;
import cn.itsource.core.SMSTemplate;
import cn.itsource.pojo.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.pojo.myenum.DriverAuthEnum;
import cn.itsource.pojo.ro.DriverAuthMaterialRo;
import cn.itsource.pojo.ro.RealAuthRo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IDriverAuthMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author whale
 * @since 2024-07-02
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
   @Autowired
    private SMSTemplate smsTemplate;
    @Override
    public void realAuth(RealAuthRo realAuthRo) {
        DriverAuthMaterial byId = super.getById(realAuthRo.getId());
        if(realAuthRo.getApprove()){
            //当状态为true的时候
            byId.setRealAuthStatus(DriverAuthEnum.AUTH_SUCCESS.getStatus());
            auth(byId, DriverAuthEnum.AUTH_SUCCESS, realAuthRo);
            //发送短信
            String phone = byId.getPhone();
            smsTemplate.sendMessage(phone,"恭喜你审核通过");
        }else {
            auth(byId, DriverAuthEnum.AUTH_NO_PASS, realAuthRo);
        }
    }

    @Override
    public void realAuthCancel(RealAuthRo realAuthRo) {
        DriverAuthMaterial material = super.getById(realAuthRo.getId());
        material.setRealAuthStatus(DriverAuthEnum.TO_DO_AUTH.getStatus());
        material.setAuditTime(new Date());
        material.setAuditUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        material.setAuditRemark(realAuthRo.getRemark());
        material.setUpdateTime(new Date());
        super.updateById(material);
    }

    @Override
    public Boolean getDriverStatus() {
        DriverAuthMaterial byId = super.getById(Long.valueOf(StpUtil.getLoginId().toString()));
        return  byId != null && byId.getRealAuthStatus() == DriverAuthEnum.AUTH_SUCCESS.getStatus();
    }

    @Override
    public void save(DriverAuthMaterialRo driverAuthMaterialRo) {
        DriverAuthMaterial driverAuthMaterial =
                DriverAuthMaterialRo.toDriverAuthMaterial(driverAuthMaterialRo);
        //实名认证
        //审核状态
        driverAuthMaterial.setRealAuthStatus(DriverAuthEnum.TO_DO_AUTH.getStatus());
        super.save(driverAuthMaterial);
    }

    private void auth(DriverAuthMaterial material, DriverAuthEnum authSuccess, RealAuthRo realAuthRo) {
        material.setRealAuthStatus(authSuccess.getStatus());
        material.setAuditTime(new Date());
        material.setAuditUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        material.setAuditRemark(realAuthRo.getRemark());
        material.setDriverId(Long.valueOf(realAuthRo.getId()));
        super.updateById(material);
    }
}
